﻿using FreesqlLearn;
using System;
using System.Reflection;
using System.Threading.Tasks;

namespace FreeSqlLearn
{
    class Program
    {
        static IFreeSql fsql_tongtai = new FreeSql.FreeSqlBuilder()
            .UseConnectionString(FreeSql.DataType.SqlServer, "data source=rm-2ze3ckoktew392y6qno.sqlserver.rds.aliyuncs.com,3433;initial catalog=jx_fx_tongtai;persist security info=True;user id=miaoyuan;password=@Zq#%i%1Fw@96HY2;") 
            //.UseLazyLoading(true)
            .UseMonitorCommand(cmd => {
                Console.WriteLine(cmd.CommandText);

            },(cmd,res)=> {
                
                Console.WriteLine(res);
            
            })
            .Build();

        static IFreeSql fsql_luxiang = new FreeSql.FreeSqlBuilder()
            .UseConnectionString(FreeSql.DataType.SqlServer, "data source=rm-2ze3ckoktew392y6qno.sqlserver.rds.aliyuncs.com,3433;initial catalog=jx_luxiangDB;persist security info=True;user id=miaoyuan;password=@Zq#%i%1Fw@96HY2;")
            //.UseLazyLoading(true)
            .UseMonitorCommand(cmd => {
                Console.WriteLine(cmd.CommandText);

            }, (cmd, res) => {

                Console.WriteLine(res);

            })
            .Build();

        static IFreeSql fsql_eating = new FreeSql.FreeSqlBuilder()
            .UseConnectionString(FreeSql.DataType.SqlServer, "data source=rm-2ze3ckoktew392y6qno.sqlserver.rds.aliyuncs.com,3433;initial catalog=rsj1;persist security info=True;user id=miaoyuan;password=@Zq#%i%1Fw@96HY2;")
            .UseAutoSyncStructure(true)

            .UseLazyLoading(true)

            .UseMonitorCommand(cmd => {
                Console.WriteLine(cmd.CommandText);

            }, (cmd, res) => {

                Console.WriteLine(res);

            })

            .UseNoneCommandParameter(true)
            .Build();

        static async Task Main(string[] args)
        {

            //fsql_tongtai.Aop.CurdAfter += (s, e) =>
            //{

            //    Console.WriteLine("执行完毕，共计耗时：" + e.ElapsedMilliseconds);

            //};
            //fsql_luxiang.Aop.CurdAfter += (s, e) =>
            //{

            //    Console.WriteLine("执行完毕，共计耗时：" + e.ElapsedMilliseconds);

            //};

            //Console.WriteLine("-------------------------------------------------");
            //Console.WriteLine("-------------------------------------------------");
            //{
            //    var lst = fsql_tongtai.Select<rq_jbxxb>().Where(m => m.bmrq>DateTime.Now.AddDays(-10)).OrderByDescending(f=>f.bmrq).ToList(f => new {
            //        f,
            //        cou = fsql_tongtai.Select<rq_jbxxb>().Where(m => m.bmrq.Value.Date==f.bmrq.Value.Date).Count(),


            //    });
            //    Console.WriteLine($"通泰共：{lst.Count}条数据");
            //    foreach (var item in lst)
            //    {
            //        Console.WriteLine($"当天共报名:{item.cou}，姓名：{item.f.mc}，身份证：{item.f.sfzh}，报名日期：{item.f.bmrq?.ToString("yyyy-MM-dd")}");
            //    }
            //}
            //Console.WriteLine("-------------------------------------------------");
            Console.WriteLine("-------------------------------------------------");
            {
                var lst = fsql_luxiang.Select<rq_jbxxb>().Where(m => m.bmrq > DateTime.Now.AddDays(-10)).OrderByDescending(f => f.bmrq).ToList(f => new
                {
                    f,
                    cou = fsql_luxiang.Select<rq_jbxxb>().Where(m => m.bmrq.Value.Date == f.bmrq.Value.Date).Count(),


                });


                Console.WriteLine($"路祥共：{lst.Count}条数据");
                foreach (var item in lst)
                {
                    Console.WriteLine($"当天共报名:{item.cou}，姓名：{item.f.mc}，身份证：{item.f.sfzh}，报名日期：{item.f.bmrq?.ToString("yyyy-MM-dd")}");
                }
            }

            fsql_eating.Aop.AuditValue += (s, e) => {
                if (e.Column.CsType == typeof(long)
                    && e.Property.GetCustomAttribute<SnowflakeAttribute>(false) != null
                    && e.Value.ToString() == "0")
                    e.Value = new SnowflakeId(1,1).NextId();
            };
            fsql_eating.Aop.CurdAfter += (s, e) =>
            {

                Console.WriteLine("执行完毕，共计耗时：" + e.ElapsedMilliseconds);

            };

            var student = new StudentAccount()
            {
                IdCard = "371312198912306233",
                Name = "lzl",
                Mobile = "15106613970",
                IsBinding = false,
                IsForbidden = false,
                Balance = 0

            };

            var getJxStu = await fsql_luxiang.Select<rq_jbxxb>().Where(f => f.sfzh == student.IdCard).ToOneAsync(); 
            if (getJxStu == null)
            {
                Console.WriteLine($"未找到身份证号为{student.IdCard}的学员");
            }
            else
            {
                Console.WriteLine($"找到身份证号为{student.IdCard}的学员");
            }
            var find =await fsql_eating.Select<StudentAccount>().Where(f => f.IdCard == student.IdCard).ToOneAsync();

            if (find == null)
            {
                var aa = await fsql_eating.Insert(student).ExecuteAffrowsAsync();
                Console.WriteLine($"操作完成：{aa}");
            }
            else 
            {
                Console.WriteLine($"已存在身份证号为{student.IdCard}的账号");
            }

            Console.ReadKey();
        }
    }
}
